
'use strict';

const Controller = require('egg').Controller;
const jwt = require('jsonwebtoken')

class UserController extends Controller {
  async index() {
    const { ctx } = this;
    // ctx.body = 'hello world';
    console.log(ctx.request.header.authorization, '----------')
    const token = ctx.request.headers.authorization;
    // console.log(token.replce('Bearer ', ''), 'token')
    const userInfo = jwt.verify(token, ctx.app.config.keys)
    // console.log(userInfo, 'info-----info')
    ctx.body = {
      userInfo
    }
  }

  async login() {
    const { ctx } = this;
    const { username, password } = ctx.request.body
    const token = jwt.sign({username, password}, ctx.app.config.keys, { expiresIn: '10h' })
    console.log(username, password, 'password')
    ctx.body = {
      token,
      msg: ctx.helper.success().errno
    }
  }
}

module.exports = UserController;
